package logic

import (
	"context"
	"gorm.io/gorm"
	"shop/models/mysql"

	"shop/shoprpc/internal/svc"
	"shop/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *shoprpc.UpdateRequest) (*shoprpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	shop := mysql.Shop{
		Model:    gorm.Model{ID: uint(in.Id)},
		Nickname: in.Nickname,
		Price:    float64(in.Price),
		Address:  in.Address,
		UserId:   int(in.UserId),
	}
	mysql.DB.Debug().Updates(&shop)
	return &shoprpc.UpdateResponse{
		UserId: int64(shop.ID),
	}, nil
}
